package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.data.SonHaiPatronsData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiBattleOneResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiBattleResp;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.Map;

@Cmd(code = Protocol.C2_SON_HAI_BATTLE_EGG, desc = "山海伏兽-战斗EGG")
public class CrossSonHaiBattleEggCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiCrossBattleEggReqMsg reqMsg = SonHaiProto.SonHaiCrossBattleEggReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiBattleEggRespMsg.Builder respMsg = SonHaiProto.SonHaiBattleEggRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_BATTLE_EGG, respMsg));
            return;
        }
        //战斗
        try {
            //解析出门客MAP
            Map<Integer, SonHaiPatronsData> patronsMap = SonHaiActivityPb.parsePatronsMap(reqMsg.getPatronsDataListList());
            //战斗
            SonHaiBattleResp resp = activity.eggBattle(packet.getPlayerId(), reqMsg.getRegionId(), reqMsg.getX(), reqMsg.getY(), patronsMap);
            //获取玩家
            CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
            CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
            SonHaiRegionData regionData = team.getRegion(reqMsg.getRegionId());
            SonHaiPlotData plotData = regionData.getPlotData(reqMsg.getX(), reqMsg.getY());
            //构造PB
            respMsg.setRet(0);
            respMsg.setPlotData(SonHaiActivityPb.buildSonHaiSimplePlotData(plotData));
            respMsg.setReward(resp.getReward());
            respMsg.setAddScore(resp.getAddScore());
            respMsg.setAddProgress(resp.getAddProgress());
            for (SonHaiBattleOneResp oneResp : resp.getBattleList()) {
                SonHaiProto.SonHaiBattleResult.Builder battleBuilder = SonHaiActivityPb.builderSonHaiBattleResult(oneResp);
                respMsg.addBattleResult(battleBuilder);
            }
            respMsg.setUserData(SonHaiActivityPb.buildSonHaiUserDataSimple(user, false));
            respMsg.setScoreAddRate(resp.getScoreAddRate());
            respMsg.setScoreAddValue(resp.getScoreAddValue());
            respMsg.setProgressAddRate(resp.getProgressAddRate());
            respMsg.setProgressAddValue(resp.getProgressAddValue());
            respMsg.setAuraScoreAddRate(resp.getAuraScoreAddRate());
            respMsg.setAuraScoreAddValue(resp.getAuraScoreAddValue());
            respMsg.addAllPatronsIds(reqMsg.getPatronsIdsList());
            respMsg.setActivityId(reqMsg.getActivityId());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_SON_HAI_BATTLE_EGG_FROM_CROSS, respMsg));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_BATTLE_EGG, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
